/*
 *  Copyright (C) 2011  social-tourist.org
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.socialtourist.web.client;

import org.socialtourist.web.client.widgets.MenuWidget;
import org.socialtourist.web.client.widgets.SocialTouristContent;
import org.socialtourist.web.client.widgets.StatusBarWidget;
import org.socialtourist.web.client.widgets.editors.PageSocialProjectEditor;
import org.socialtourist.web.client.widgets.pages.PageLogin;
import org.socialtourist.web.client.widgets.pages.PageMap;
import org.socialtourist.web.client.widgets.pages.PagePictureUpload;
import org.socialtourist.web.shared.data.SocialProject;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class ContentContainer implements ValueChangeHandler<String> {
	public enum sections {
		add_project, edit_project, edit_needs, upload_picture, login, logout, start
	};

	private RootPanel header;
	private RootPanel contentPanel;
	private RootPanel footer;

	private SocialTouristServiceAsync service;
	private String session;
	private MenuWidget menuBar;
	private PageMap startPage;

	public ContentContainer(RootPanel header, RootPanel content,
			RootPanel footer, SocialTouristServiceAsync service) {
		this.header = header;
		this.contentPanel = content;
		this.footer = footer;

		this.service = service;
		this.session = null;

		History.addValueChangeHandler(this);

		initDefault();
	}

	private void initDefault() {
		menuBar = createMenu();
		header.add(menuBar);
		footer.add(createFooter());

	}

	public MenuWidget createMenu() {
		return new MenuWidget(this);
	}

	private Widget createFooter() {
		return new StatusBarWidget(this);
	}

	public void setContent(SocialTouristContent newContent) {
		contentPanel.clear();
		contentPanel.add(newContent);
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		// TODO History handling.

	}

	public void goTo(sections choice) {
		switch (choice) {
		case start:
			gotoStartPage();
			break;

		case login:
			gotoLoginPage();
			break;
			
		case upload_picture:
			gotoUploadPage();
			break;
			
		case edit_project:
			gotoEditProject();
			break;

		default:
			throw new RuntimeException("Not implemented yet: "
					+ choice.toString());

		}

	}

	private void requireAdmin() {
		//TODO
		if (session != null)
			return;
		
		String log = "Need to be logged in as admin"; 
		GWT.log(log);
		throw new RuntimeException(log);
		
	}
	
	private void gotoEditProject() 
	{
		requireAdmin();
		setContent(createEditProject());
		
	}


	private SocialTouristContent createEditProject() 
	{
		SocialProject edited = new SocialProject();
		edited.setName("Test-Name");
		SocialTouristContent newContent = new PageSocialProjectEditor(this, edited);
		
		return newContent;
	}
	
	private void gotoUploadPage() 
	{
		requireAdmin();
		setContent(createUploadPage());
		
	}


	private SocialTouristContent createUploadPage() 
	{
		SocialTouristContent newContent = new PagePictureUpload(this);
		
		return newContent;
	}

	void gotoStartPage() {
		setContent(createStartPage());
	}

	private SocialTouristContent createStartPage() {
		if (startPage == null) {
			startPage = new PageMap(this, service);
			service.getProjects(startPage);
		}

		return startPage;
	}

	private void gotoLoginPage() {
		if (session == null) {
			SocialTouristContent newContent = createLoginPage();
			setContent(newContent);
		}
	}

	private SocialTouristContent createLoginPage() {
		final class LoginHandler implements ClickHandler, AsyncCallback<String> {
			private PageLogin menu;

			public LoginHandler(PageLogin menu) {
				this.menu = menu;
			}

			@Override
			public void onFailure(Throwable caught) {
				menu.setErrorMessage("Failure: " + caught.getLocalizedMessage());

			}

			@Override
			public void onSuccess(String result) {
				menu.setErrorMessage("Success: " + result);
				session = result;
				menuBar.enableAdminEntries();
				gotoStartPage();

			}

			@Override
			public void onClick(ClickEvent event) {
				menu.login(service, this);

			}

		}

		PageLogin login = new PageLogin(this);
		LoginHandler handler = new LoginHandler(login);
		login.addClickHandler(handler);

		return login;
	}

}
